package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ArticleHomeService;
import com.usian.common.constants.article.ArticleConstans;
import com.usian.model.article.dtos.ArticleHomeDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleHomeServiceImpl extends BaseServiceImpl<ApArticleMapper, ApArticle> implements ArticleHomeService {


    @Autowired
    private ApArticleMapper apArticleMapper;

    @Value("${file.url}")
    private String fileUrl;

    @Override
    public ResponseResult load(ArticleHomeDto dto, Short loadType) {

        if (dto == null) return errorParam;
        Date maxBehotTime = dto.getMaxBehotTime();
        Date minBehotTime = dto.getMinBehotTime();
        String tag = dto.getTag();
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            dto.setSize(20);
        }
        if (!loadType.equals(ArticleConstans.LOADTYPE_LOAD_MORE) && !loadType.equals(ArticleConstans.LOADTYPE_LOAD_NEW)) {
            loadType = ArticleConstans.LOADTYPE_LOAD_MORE;
        }
        if (StringUtils.isEmpty(tag)) {
            dto.setTag(ArticleConstans.DEFAULT_TAG);
        }
        if (maxBehotTime == null) dto.setMaxBehotTime(new Date());
        if (minBehotTime == null) dto.setMinBehotTime(new Date());

        List<ApArticle> list= apArticleMapper.loadArticleList(dto,loadType);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setHost(fileUrl);
        return responseResult;
    }

}
